package com.test.demos.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.test.demos.pojo.dto.MedicalInstitutionsDTO;
import com.test.demos.pojo.entity.MedicalInstitutions;
import com.test.demos.service.MedicalInstitutionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.test.demos.utils.Result;

/**
 * 体检机构
 *
 * @author twt
 * @since 2025-04-14 16:26:44
 */
@CrossOrigin
@RestController
@RequestMapping("medicalInstitutions")
public class MedicalInstitutionsController {

    /**
     * 服务对象
     */
    @Autowired
    private MedicalInstitutionsService medicalInstitutionsService;




    /**
     * 通过ID查询单条数据
     *
     * @param institutionId 主键
     * @return 实例对象
     */
    @SaCheckLogin
    @GetMapping("{id}")
    public Result<MedicalInstitutions> queryById(@PathVariable("id") Integer institutionId) {
        return Result.ok(medicalInstitutionsService.queryById(institutionId));
    }


    /**
     * 更新数据
     *
     * @param dto 实例对象
     */
    @SaCheckPermission(orRole = {"admin", "emp"})
    @PutMapping
    public Result update(@RequestBody MedicalInstitutionsDTO dto) {
        medicalInstitutionsService.update(dto);
        return Result.ok();
    }

    /**
     * 根据id获取该机构下的所有的套餐
     */
    @SaCheckLogin
    @GetMapping("getAllPackage/{id}")
    public Result getAllPackage(@PathVariable("id") Integer institutionId){
        return Result.ok(medicalInstitutionsService.getAllPackage(institutionId));
    }

    /**
     * 查询全部数据
     *
     * @return 实例对象
     */
    @SaCheckRole("admin")
    @GetMapping()
    public Result queryAll() {
        return Result.ok(medicalInstitutionsService.queryAll());
    }

    /**
     * 通过主键删除数据
     *
     * @param institutionId 主键
     * @return 是否成功
     */
    @SaCheckRole("admin")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id") Integer institutionId) {
        medicalInstitutionsService.deleteById(institutionId);
        return Result.ok();
    }

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @SaCheckPermission(orRole = {"admin", "emp"})
    @PostMapping
    public Result add(@RequestBody MedicalInstitutionsDTO dto) {
        medicalInstitutionsService.add(dto);
        return Result.ok();
    }
}
